package com.zjj.lbw.algorithm.sort;

import java.util.Arrays;

/**
 * @author zhanglei.zjj
 * @description 冒泡排序算法
 * @date 2023/3/25 15:05
 */
public class BubbleSort {
    public static void main(String[] args) {
        int[] unsortedArr = new int[]{4, 2, 28, 33, 12, 1};
        bubbleSort(unsortedArr);
    }

    static void bubbleSort(int[] arr) {
        System.out.println("排序前元素顺序：" + Arrays.toString(arr));

        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 -i ; j++) {
                if (arr[j + 1] < arr[j]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println("升序排序后元素顺序：" + Arrays.toString(arr));
    }
}
